package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.ApplyForEntryStatusModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/23.
 * 申请入住状态
 */

public class ApplyForEntryStatusActivity  extends Activity implements View.OnClickListener ,Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.justification)
    TextView justification;//状态
    @Bind(R.id.title)
    TextView title;//类型
    @Bind(R.id.time)
    TextView time;//时间
    @Bind(R.id.modification)
    TextView modification;//修改

    @Bind(R.id.rejected)
    TextView rejected;//驳回


    @Bind(R.id.rejected_deleting_layout)
    LinearLayout rejected_deleting_layout;//驳回

    Context context;
    private MyProgressDialog mDialog;
    List<ApplyForEntryStatusModel> getmybusiness;
    String toexamine="";//状态
    String Judge_interface="0";//判断哪个接口   0、获取商户申请状态 1、删除商户
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_for_entry_status);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        modification.setOnClickListener(this);
        rejected.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        processmybusiness();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.modification:
                if (toexamine.equals("4")){
                    Intent intent=new Intent();
                    intent.setClass(context,ApplicationActivity.class);
                    intent.putExtra("pos","1");
                    intent.putExtra("id",getmybusiness.get(0).getId());
                    startActivity(intent);
                }else {
                    processdeleting();
                }

                break;
            case R.id.rejected:
                startActivity(new Intent(context,MessageActivity.class));
                break;
        }

    }
    //申请商家
    private void processmybusiness(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            Judge_interface="0";
            HttpClient.mybusiness(this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //删除商家
    private void processdeleting(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            Judge_interface="1";
            HttpClient.delbusiness(getmybusiness.get(0).getId(),this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote!=null||getnote.size()>0){
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
            if (error.equals("200")){
                if (Judge_interface.equals("0")){
                    String content = getnote.get(0).getResult();
                    getmybusiness = JsonTools.getmybusiness(content);
                    if (getmybusiness!=null||getmybusiness.size()>0){
                        processData();
                    }
                }else {
                    finish();
                    ToastUtils.show(context, mssage);
                }
            }else if (error.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, mssage);
            }else {
                ToastUtils.show(context, mssage);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+ex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+cex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
    private void processData(){
        ApplyForEntryStatusModel applyForEntryStatusModel = getmybusiness.get(0);
        if (applyForEntryStatusModel.getToexamine().equals("2")){
            justification.setText("审核中");
            rejected_deleting_layout.setVisibility(View.GONE);
        }else if (applyForEntryStatusModel.getToexamine().equals("4")){
            justification.setText("被驳回");
            rejected_deleting_layout.setVisibility(View.VISIBLE);
            toexamine=applyForEntryStatusModel.getToexamine();
        }else if (applyForEntryStatusModel.getToexamine().equals("5")){
            justification.setText("被回收");
            rejected.setText("回收理由");
            rejected_deleting_layout.setVisibility(View.VISIBLE);
            modification.setText("删除");
            toexamine=applyForEntryStatusModel.getToexamine();
        }
        String type="";
        if (applyForEntryStatusModel.getMerchanttype().equals("1")){
            type="房产商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("2")){
            type="购车商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("3")){
            type="干洗商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("4")){
            type="开锁商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("5")){
            type="果蔬商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("6")){
            type="苦力商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("7")){
            type="快递商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("8")){
            type="家政商家";
        }else if (applyForEntryStatusModel.getMerchanttype().equals("9")){
            type="洗车商家";
        }
        title.setText("申请类型："+type);
        time.setText("申请时间："+Sundry.getStrTime(2,applyForEntryStatusModel.getApplytime()));
    }
}
